package com.hn.shopadmin.dto;

import com.google.common.base.Converter;
import com.hn.kiss.modules.sys.shopcommon.validdator.IsIdNo;
import com.hn.kiss.modules.sys.shopdb.entity.User;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.Email;
import javax.validation.constraints.Size;
import java.util.Date;

@ApiModel(description = "用户注册实体")
@Data
public class UserRegisterDTO {

    @ApiModelProperty(value = "密码", required = true)
    private String password;

    @ApiModelProperty(value = "昵称", required = true)
    private String nickname;

    @ApiModelProperty(value = "姓名")
    private String name;

    @ApiModelProperty(value = "性别", required = true)
    private String sex;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(value = "出生日期")
    private Date dateofbirth;

    @IsIdNo
    @ApiModelProperty(value = "身份证号")
    private String idno;

    @Email
    @ApiModelProperty(value = "邮箱")
    private String emall;

    @Size(min = 1,max = 11)
    @ApiModelProperty(value = "手机号", required = true)
    private String phone;

    @ApiModelProperty(value = "地址省")
    private String addressProvince;

    @ApiModelProperty(value = "地址市")
    private String addressCity;

    @ApiModelProperty(value = "地址县")
    private String addressCounty;

    @ApiModelProperty(value = "地址详细")
    private String addressDetail;

    public User convertToUser(){
        UserRegisterDTOConver userRegisterDTOConver = new UserRegisterDTOConver();
        User user = userRegisterDTOConver.doForward(this);
        return user;
    }
    public UserRegisterDTO convertFor(User user){
        UserRegisterDTOConver userRegisterDTOConver = new UserRegisterDTOConver();
        UserRegisterDTO userRegisterDTO = userRegisterDTOConver.doBackward(user);
        return userRegisterDTO;
    }

    public static class UserRegisterDTOConver extends Converter<UserRegisterDTO, User>{

        @Override
        protected User doForward(UserRegisterDTO userRegisterDTO) {
            User user = new User();
            BeanUtils.copyProperties(userRegisterDTO, user);
            return user;
        }

        @Override
        protected UserRegisterDTO doBackward(User user) {
            UserRegisterDTO userRegisterDTO = new UserRegisterDTO();
            BeanUtils.copyProperties(user, userRegisterDTO);
            return userRegisterDTO;

        }
    }


}
